//
//  DynamicViewController.m
//  QQ界面
//
//  Created by apple on 16/10/22.
//  Copyright © 2016年 LWB. All rights reserved.
//

#import "DynamicViewController.h"
#import "MoreViewController.h"
#import "dynamicFrame.h"
#import "dynamicTableViewCell.h"
#import "dynamicGroupModels.h"
@interface DynamicViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)UIView *norView;
@property (nonatomic,strong)NSMutableArray *dynamFrame;
@end

@implementation DynamicViewController

-(NSMutableArray *)dynamFrame{
    if (_dynamFrame ==nil) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"dynamicPilst.plist" ofType:nil];
        NSArray *arr = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *arr2 = [NSMutableArray array];
        for (NSDictionary *dict in arr) {
            dynamicGroupModels *dynam = [dynamicGroupModels dynamicGroupModelsWithDict:dict];
            [arr2 addObject:dynam];
        }
        _dynamFrame =arr2;
    }
    return _dynamFrame;
}
- (void)viewDidLoad {
    [super viewDidLoad];
      self.view.backgroundColor = [[UIColor alloc]initWithRed:249/255.0 green:249/255.0 blue:251/255.0 alpha:1];
    //self.edgesForExtendedLayout = UIRectEdgeNone;
    [self setrightItem];
    [self setUpViewAddControls];
    _tbView = [[UITableView alloc]initWithFrame:CGRectMake(0,28, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)style:UITableViewStyleGrouped];
    _tbView.delegate = self;
    _tbView.dataSource = self;
    [self.view addSubview:_tbView];
    _tbView.sectionHeaderHeight =0;
//    _tbView.sectionFooterHeight = 0;
    self.automaticallyAdjustsScrollViewInsets =    NO;
    [self dynamFrame];
    // Do any additional setup after loading the view.
}
-(void)setUpViewAddControls{
    self.norView = [[UIView alloc]initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, 140)];
  //  [self.view addSubview:_norView];
    [self setThreeButton];
    [self setSearchBar];
}
-(void)setThreeButton{
    UIButton *trendsBut = [[UIButton alloc]initWithFrame:CGRectMake(0, 60, [UIScreen mainScreen].bounds.size.width/3, 80)];
    [trendsBut setBackgroundImage:[UIImage imageNamed:@"trends-1"] forState:UIControlStateNormal];
    [trendsBut addTarget:self action:@selector(trendsAction) forControlEvents:UIControlEventTouchUpInside];
    [self.norView addSubview:trendsBut];
    UIButton *nearBut = [[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3,60, [UIScreen mainScreen].bounds.size.width/3, 80)];
    [nearBut setBackgroundImage:[UIImage imageNamed:@"near－1"] forState:UIControlStateNormal];
    [nearBut addTarget:self action:@selector(nearAction) forControlEvents:UIControlEventTouchUpInside];
    [self.norView addSubview:nearBut];
    UIButton *interesttribeBut = [[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3*2,60, [UIScreen mainScreen].bounds.size.width/3, 80)];
    [interesttribeBut setBackgroundImage:[UIImage imageNamed:@"Interest－tribe"] forState:UIControlStateNormal];
    [interesttribeBut addTarget:self action:@selector(interesttribeAction) forControlEvents:UIControlEventTouchUpInside];
    [self.norView addSubview:interesttribeBut];
}
-(void)trendsAction{
    
}
-(void)nearAction{
    
}
-(void)interesttribeAction{
    
}
-(void)setSearchBar{
    UISearchBar *searBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, 60)];
    searBar.backgroundImage = [UIImage imageNamed:@"UISearchBar_p"];
    searBar.searchBarStyle = UISearchBarStyleMinimal ;
    searBar.placeholder = @"搜索电影/音乐/商品...";
    [self.norView addSubview:searBar];
}
-(void)setrightItem{
    self.navigationItem.title = @"动态";
    UIBarButtonItem *rightBarBut = [[UIBarButtonItem alloc]initWithTitle:@"更多" style:UIBarButtonItemStylePlain target:self action:@selector(MoreAtion)];
    self.navigationItem.rightBarButtonItem = rightBarBut;

}
-(void)MoreAtion{
    MoreViewController *more = [[MoreViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:more animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
#pragma mark  -Table代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==0) {
        return 1;
    }
    dynamicGroupModels *dygroup = [[dynamicGroupModels alloc]init];
    dygroup =_dynamFrame[section-1];
       return dygroup.groupFrame.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSLog(@"%u",_dynamFrame.count+1);
    return _dynamFrame.count+1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([indexPath section]==0) {
        return 140;
    }
//    dynamicGroupModels *dygroup =_dynamFrame[indexPath.section-1];
//    dynamicFrame *dynFrame = dygroup[indexPath.row];
    return 55;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID =@"MyCell";
    
    dynamicTableViewCell *cell = [_tbView dequeueReusableCellWithIdentifier:ID];
    if (cell ==nil) {
        cell = [[dynamicTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        NSLog(@"%ld",(long)[indexPath section]);
    }
    if ([indexPath section]!=0) {
        dynamicGroupModels *dygroup =_dynamFrame[indexPath.section-1];
        cell.dynam =dygroup.groupFrame[indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     }else{
         [cell.contentView addSubview:_norView];

    }
    
    return cell;
}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    if (section!=0) {
//        return 30;
//    }
//    return 0;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
